//
//  PSFileListController.m
//  photoSep
//
//  Created by David Michaeli on 04/10/12.
//  Copyright (c) 2012 David Michaeli. All rights reserved.
//

#import "PSFileListController.h"
#import "PSFileListNode.h"
#import "ImageAndTextCell.h"
#import "PSFileListNode.h"

/*****************************************************
 * Global definitions for "PSFileListController"
 */

/*****************************************************
 * Private properties for "PSFileListController"
 */
@interface PSFileListController (private)


@end

/*****************************************************
 * Implementation for "PSFileListController"
 */
@implementation PSFileListController

- (id)init
{
    self = [super init];
    if (self) {

    }
    return self;
}

- (void)dealloc
{
    [_nodes release];
    [super dealloc];
}

- (void) awakeFromNib
{
    [super awakeFromNib];

    _removeItemButtonSel = @selector(removeItemPressed:);
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    _count = 0;
    _nodes = [[NSMutableArray alloc] init];
    _removeItemButtonSel = @selector(removeItemPressed:);
    _setProgressValSel= @selector(setProcessingProgress:);

    
    return self;
}


@synthesize tableView = _tableView;
@synthesize removeItemButtonSel = _removeItemButtonSel;
@synthesize setProgressValSel = _setProgressValSel;
@synthesize currentSelectedNode = _currentSelectedNode;

- (IBAction)addRow:(id)sender
{
    PSFileListNode *node = [[[PSFileListNode alloc] initWithName:[NSString stringWithFormat:@"Num%d", _count]] retain];
    [node setImage:[NSImage imageNamed:@"pola_stack_128-custom.png"]];
    
    // save the current node button
    
    // This specifies the selector and target of the controller when imageListItem is pressed
    [node setRemoveButtonSelector: _removeItemButtonSel];
    [node setSetProgressSelector: _setProgressValSel];
    [node setSelectorTarget: self];
    
    [_nodes addObject:node];
    _count++;
    
    [_tableView reloadData];
}

- (IBAction)removeRow:(id)sender
{
    NSIndexSet* set = [_tableView selectedRowIndexes];
    [_nodes removeObjectsAtIndexes:set];
    [(NSTableView*)_tableView removeRowsAtIndexes:set withAnimation:NSTableViewAnimationSlideLeft];
 }

- (void)removeItemPressed:(id)sender
{
    NSButton *button = (NSButton *)sender;
    PSFileListNode* node;
    BOOL found = NO;
    
    // find the right node
    for (node in _nodes)
    {
        if (button == [node removeButtonSource])
        {
            found = YES;
            [_nodes removeObject:node];
            break;
        }
    }
    
    if (_currentSelectedNode == node)
        self.currentSelectedNode = nil;
    
    [_tableView reloadData];
    //ImageAndTextCell* cell = (ImageAndTextCell*)([button superview]);
}

- (void)setProcessingProgress:(id)sender
{
    PSFileListNode *node = (PSFileListNode*)sender;
    NSUInteger rowNum = [_nodes indexOfObject:node];
    NSIndexSet *rowSet = [[NSIndexSet alloc] initWithIndex:rowNum];
    NSIndexSet *colSet = [[NSIndexSet alloc] initWithIndex:0];
    
    [_tableView reloadDataForRowIndexes:rowSet columnIndexes:colSet];
}

/**************************************************************************************
 * Data-source methods
 */
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return [_nodes count];
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSMutableArray* nodesArr = _nodes;
    PSFileListNode *item = [nodesArr objectAtIndex:row];
    
    ImageAndTextCell *result = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
    
    result.imageView.image = item.image;
    result.nameText.stringValue = item.name;
    [[result removeButton] setTarget:item.selectorTarget];
    [[result removeButton] setAction:item.removeButtonSelector];
    
    
    if (item.processProgress < 100.0)
    {
        result.detailText.stringValue = @"Processing Image...";
        [[result progress] setHidden:NO];
        [[result progress] setDoubleValue:item.processProgress];
    }
    else
    {
        [[result progress] setHidden:YES];
        result.detailText.stringValue = @"Found 5 Images";
    }

    [[result removeButton] setTransparent:YES];
    [[result removeButton] setEnabled:NO];
    [item setRemoveButtonSource: [result removeButton]];
    
    return result;
}

- (void) acceptFilenameDrag:(id)val
{
    NSString* filename = (NSString*)val;
    NSString* extension = [[filename pathExtension] lowercaseString];
    NSSet *validImageExtensions = [NSSet setWithArray:[NSImage imageFileTypes]];
    
    
    // check if the extension is one of the possible
    if ([validImageExtensions containsObject:extension])
    {
        // import the filename
        PSFileListNode *newNode = [[PSFileListNode alloc] initWithPath:filename];
        
        [newNode setRemoveButtonSelector:_removeItemButtonSel];
        [newNode setSelectorTarget:self];
        [newNode setSetProgressSelector:_setProgressValSel];
        [newNode setImage:[NSImage imageNamed:@"pola_stack_128-custom.png"]];
        [_nodes addObject:newNode];
        
        [_tableView reloadData];
    }
    
}

- (void) tableViewSelectionDidChange: (NSNotification *) notification
{
    int row;
    row = (int)[_tableView selectedRow];
    
    if (row == -1) {
        self.currentSelectedNode = nil;
        //do stuff for the no-rows-selected case
    } else {
        self.currentSelectedNode = [_nodes objectAtIndex:row];
        //do stuff for the selected row
    }
    
} 

@end

